/*************************************************************************
 * File Name:    Subsets.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: Sat 02 Nov 2013 06:55:57 PM CST
 * 
 * Description:  
 |-----------------------------------------------------------------------
 | Problem: Subsets Total
 | Given a set of distinct integers, S, return all possible subsets.
 |
 | Note:
 | Elements in a subset must be in non-descending order.
 | The solution set must not contain duplicate subsets.
 | For example,
 | If S = [1,2,3], a solution is:
 |
 | [
 |   [3],
 |   [1],
 |   [2],
 |   [1,2,3],
 |   [1,3],
 |   [2,3],
 |   [1,2],
 |   []
 | ]
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <iomanip>

using namespace std;

class Solution {
public:
    vector<vector<int> > subsets(vector<int> &S)
    {
        int N = S.size();
        int lim = 1 << N;
        vector<vector<int> > ans;
        vector<int> t;
        
        for (int i = 0; i < lim; ++i) {
            t.clear();
            for (int j = 0; j < N; ++j) {
                if (i & (1 << j)) t.push_back(S[j]);
            }
            sort(t.begin(), t.end());
            ans.push_back(t);
        }
        return ans;
    }
};
